package net.gxf.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import net.gxf.common.etf.Stock;


/**
 * 根据股票代码查询股票信息，可返回全部信息，可单独返回价格等...
 * @author maybe
 * 
 */
public class StockQueryUtil {

	/**
	 * 
	 * @param code
	 * @param type
	 *            上证sh 深证sz
	 * @return
	 */
	public static Stock queryByCode(String code, String type) {
		StringBuffer document = new StringBuffer();
		Stock info = new Stock();
		try {
			if (type == null || type.length() == 0)
				return null;
			URL url = new URL("http://hq.sinajs.cn/list=" + type + code);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line = null;
			while ((line = reader.readLine()) != null)
				document.append(line + " ");
			reader.close();
			String[] resString = document.toString().split("=");
			String resString2 = resString[1].substring(1, resString[1].length() - 3);

			String[] infoStrings = resString2.split(",");
			info.setCode(code);
			info.setName(infoStrings[0]);
			if (Float.valueOf(infoStrings[3]) != 0) {// 该股票停牌，计算采用昨日收盘价
				info.setCurrentPrice(Float.valueOf(infoStrings[3]));
			} else {
				info.setCurrentPrice(Float.valueOf(infoStrings[2]));
			}

			// System.out.println("number :" + number);
			// System.out.println("name :" + infoStrings[0]);
			// System.out.println("urrentPric :" + infoStrings[3]);
			// System.out.println(resString2);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return info;

	}

	public static float queryPriceByCode(String code, String type) {
		/*StringBuffer document = new StringBuffer();
		try {
			if (type == null || type.length() == 0)
				throw new IllegalArgumentException("类型错误");
			URL url = new URL("http://hq.sinajs.cn/list=" + type + code);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line = null;
			while ((line = reader.readLine()) != null)
				document.append(line + " ");
			reader.close();
			String[] resString = document.toString().split("=");
			String resString2 = resString[1].substring(1, resString[1].length() - 3);

			if (resString2.length() < 3)
				System.out.println(code + " : " + type);
			String[] infoStrings = resString2.split(",");

			if (Float.valueOf(infoStrings[3]) != 0) {// 该股票停牌，计算采用昨日收盘价
				return Float.valueOf(infoStrings[3]);

			} else {
				return Float.valueOf(infoStrings[2]);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		return queryByCode(code, type).getCurrentPrice();

	}
	
	public static float queryLastPriceByCode(String code,String type){
		StringBuffer document = new StringBuffer();
		try {
			if (type == null || type.length() == 0)
				throw new IllegalArgumentException("类型错误: 可接受的类型为 sh 或者 sz");
			URL url = new URL("http://hq.sinajs.cn/list=" + type + code);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line = null;
			while ((line = reader.readLine()) != null)
				document.append(line + " ");
			reader.close();
			String[] resString = document.toString().split("=");
			String resString2 = resString[1].substring(1, resString[1].length() - 3);

			if (resString2.length() < 3)
				System.out.println(code + " : " + type);
			String[] infoStrings = resString2.split(",");

			//if (Float.valueOf(infoStrings[3]) != 0) {// 该股票停牌，计算采用昨日收盘价
				//return Float.valueOf(infoStrings[3]);

			//} else {
				return Float.valueOf(infoStrings[2]);
			//}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}

}
